package com.defysoft.waukids;

import java.util.List;

import com.defysoft.waukids.intro.Intro_Intro_A;
import com.defysoft.waukids.notice.Notice_Main_A;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

public class GCMIntentService extends IntentService {
	private static final String TAG = "GCMIntentService";
	private SharedPreferences check_notice;
	SharedPreferences sharedPreferences;

	/**
	 * Constructor
	 */
	public GCMIntentService() {
		super(BasicInfo.PROJECT_ID);
		Log.d(TAG, "GCMIntentService() called.");
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		boolean onapp = true; // 어플 켜짐 유무 확인
	
		ActivityManager am = (ActivityManager) getApplicationContext()
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
		ComponentName topActivity = taskInfo.get(0).topActivity; // 어플이 켜졌는지 확인을
																	// 위해 현재켜진
																	// 어플의 액티비티
																	// 이름 얻음

		if (topActivity.getPackageName().equals("com.defysoft.waukids")) {
			onapp = true;
		} else {
			onapp = false;
		}
		
		
		
		sharedPreferences = getSharedPreferences("notifications", MODE_PRIVATE);
		if (sharedPreferences.getString("push_push", "0").equals("0")) {
			int sound = sharedPreferences.getString("push_sound", "0").equals(
					"0") ? Notification.DEFAULT_SOUND: 0;
			
			int vibration = sharedPreferences.getString("push_vibration", "0")
					.equals("0") ? Notification.DEFAULT_VIBRATE : 0;

			NotificationManager mNotificationManager = (NotificationManager) this
					.getSystemService(Context.NOTIFICATION_SERVICE);
			
			NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
					this);
		
			// 메시지 좌측의 아이콘
			mBuilder.setSmallIcon(R.drawable.ic_launcher);
			mBuilder.setContentTitle(intent.getStringExtra(PublicConstant.PUSH_NAME));
			mBuilder.setAutoCancel(true);
			mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(intent.getStringExtra(PublicConstant.PUSH_SUB)));
			mBuilder.setContentText(intent.getStringExtra(PublicConstant.PUSH_SUBNAME));
			mBuilder.setDefaults(sound | vibration);
			// 상태gcm
			if (intent.getStringExtra(PublicConstant.PUSH_SENDTYPE).equals(
					PublicConstant.GCM_STATE_CODE)) {
				if(!onapp){
					mBuilder.setContentIntent(PendingIntent.getActivity(getApplicationContext(),0, new Intent(getApplicationContext(), Intro_Intro_A.class), PendingIntent.FLAG_UPDATE_CURRENT));
				}else {
					Intent sendintent=new Intent("com.defysoft.waukids.TOAST_MESSAGE");
					sendBroadcast(sendintent);
				}
				mNotificationManager.notify(Integer.parseInt(intent
						.getStringExtra(PublicConstant.PUSH_CODE)),
						mBuilder.build());

			}
			// 현장학습gcm
			else if (intent.getStringExtra(PublicConstant.PUSH_SENDTYPE)
					.equals(PublicConstant.GCM_FIELD_CODE)) {
				if(!onapp){
					mBuilder.setContentIntent(PendingIntent.getActivity(getApplicationContext(),0, new Intent(getApplicationContext(), Intro_Intro_A.class), PendingIntent.FLAG_UPDATE_CURRENT));
				}
				if (sharedPreferences.getString("push_spot", "0").equals("0")) {
					mNotificationManager.notify(Integer.parseInt(intent
							.getStringExtra(PublicConstant.PUSH_CODE)),
							mBuilder.build());
				}

			}
			// 공지사항gcm
			else if (intent.getStringExtra(PublicConstant.PUSH_SENDTYPE)
					.equals(PublicConstant.GCM_NOTICE_CODE)) {
				check_notice= getSharedPreferences("check_notice", MODE_PRIVATE);
				check_notice.edit().putInt("notice_count", 1).commit();
				if(!onapp){
					mBuilder.setContentIntent(PendingIntent.getActivity(getApplicationContext(),0, new Intent(getApplicationContext(), Intro_Intro_A.class), PendingIntent.FLAG_UPDATE_CURRENT));
				}else {
					mBuilder.setContentIntent(PendingIntent.getActivity(getApplicationContext(),0, new Intent(getApplicationContext(), Notice_Main_A.class).putExtra("class", intent.getStringExtra("class")), PendingIntent.FLAG_UPDATE_CURRENT));
				}
				if (sharedPreferences.getString("push_notice", "0").equals("0")) {
					mNotificationManager.notify(Integer.parseInt(intent
							.getStringExtra(PublicConstant.PUSH_CODE)),
							mBuilder.build());
				}
				Intent sendintent=new Intent("com.defysoft.waukids.TOAST_MESSAGE").putExtra("notice", true);
				sendBroadcast(sendintent);
			}
		}
	}

}